import { AppTask, AppTaskQueue } from "../app.task.class";
import { equals } from "ramda";
import { CmdRegisterHandlerTask } from "./handler/cmd.register.handler";
import { CmdHealthTestHandler } from "./handler/cmd.health-test.handler";
import { CmdParams } from "./cmd.params";
import { logger, loggerStdout } from "../../configs/log.config";
import { AppServer } from "../app.server";

/**
 * 命令行任务
 */
export class CmdTask extends AppTask {
  constructor(private __args: string[]) {
    super();
  }
  __pause(): Promise<boolean> {
    return Promise.resolve(false);
  }

  __restore(): Promise<boolean> {
    return Promise.resolve(false);
  }

  async __run(): Promise<void> {
    const equalsCmd = equals(CmdParams.cmd);
    if (equalsCmd("register")) {
      await new CmdRegisterHandlerTask(true).start();
    } else if (equalsCmd("health-test")) {
      await new CmdHealthTestHandler().start();
    } else if (equalsCmd("")) {
      await new CmdRegisterHandlerTask(false).start();
      await this.__startServer();
    } else {
      loggerStdout.error("无法识命", CmdParams.cmd);
    }
  }

  async __startServer() {
    logger.info("server start.");
    // app 管理器 (没卵用，只是方便启动）；
    const appManager = new AppTaskQueue();
    appManager.addTask(new AppServer());
    await this.__await();
  }

  __stop(): Promise<boolean> {
    return Promise.resolve(false);
  }
}
